$(function () {
    initTable();
    validateForm();
});

/**
 * 初始化表格
 */
function initTable() {

    //先销毁表格
    $('#table').bootstrapTable('destroy');

    $('#table').bootstrapTable({
        showRefresh: false,
        pagination: true,
        sidePagination: "server",
        pageSize: "30",
        pageList: "[30, 60, 120, ALL]",
        showColumns: false,
        url: baseUrl + '/postSignInfo/queryByPage',
        columns: [
            {
                checkbox: true,
                align: 'center'
            },
            {
                title: '视线状况',
                field: 'post_sign_name',
                align: 'center'
            }
        ],
        striped: true, //表格显示条纹
        search: false, //是否启用查询
        queryParamsType: "undefined",
        onLoadError: function () {
            $.confirm({
                title: '提示!',
                content: '服务器异常！',
                type: 'red',
                typeAnimated: true,
                buttons: {
                    close: {
                        text: '关闭',
                        btnClass: 'btn-red'
                    }
                }
            });
        },
        onLoadSuccess: function (data) {
            // 错误提示
            errorAlert(data);
        }
    });
}

/**
 * 表单验证
 */
function validateForm() {

    $('#editForm').bootstrapValidator({
        excluded: [':disabled'],
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: 'button[type="submit"]',
        resetFormData: true,
        fields: {
            post_sign_name: {
                validators: {
                    /*非空提示*/
                    notEmpty: {
                        message: '不能为空'
                    },
                    /*长度提示*/
                    stringLength: {
                        min: 1,
                        max: 20,
                        message: '长度为1-20'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e, data) {
        e.preventDefault();//阻止form默认事件
        var id = $('#id').val();
        var url;
        if (id == '') {
            url = baseUrl + '/postSignInfo/add';
        } else {
            url = baseUrl + '/postSignInfo/update';
        }
        $.post(
            url, {
                id: id,
                post_sign_name: $('#post_sign_name').val()
            },
            function (data) {
                if (data.success) {
                    $('#modal-postSignInfo').modal('toggle');
                    $.confirm({
                        title: '成功',
                        content: '操作完成!',
                        autoClose: 'okAction|2000',
                        escapeKey: 'okAction',
                        type: 'blue',
                        buttons: {
                            okAction: {
                                text: '关闭',
                                btnClass: 'btn-blue'
                            }
                        }
                    });
                    $('#table').bootstrapTable('refresh', {silent: true});
                } else {
                    // 错误提示
                    errorAlert(data);
                }
            }, 'json');
    });
}

/**
 * 获取选择行id
 * @returns {Array|*}
 */
function getIdSelections() {
    return $.map($('#table').bootstrapTable('getSelections'), function (row) {
        return row.id;
    });
}

/**
 * 表格项选择事件触发
 */
$('#table').on('check.bs.table uncheck.bs.table ' +
    'check-all.bs.table uncheck-all.bs.table', function () {
    $('#btnUpdate').prop('disabled', !$('#table').bootstrapTable('getSelections').length);
    $('#btnDel').prop('disabled', !$('#table').bootstrapTable('getSelections').length);
});

/**
 * 禁用按钮
 */
function disAbleButton() {
    $('#btnUpdate').prop('disabled', true);
    $('#btnDel').prop('disabled', true);
}

/**
 * 删除操作
 */
function del() {
    var ids = getIdSelections();
    $.confirm({
        title: '确认',
        content: '确认删除？',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-green',
                keys: ['enter'],
                action: function () {
                    $.post(
                        baseUrl + '/postSignInfo/del', {ids: ids.toString()},
                        function (data) {
                            if (data.success) {
                                $('#table').bootstrapTable('refresh', {silent: true});
                                // 灰按钮
                                disAbleButton();
                            } else {
                                // 错误提示
                                errorAlert(data);
                            }
                        }, 'json');
                }
            },
            close: {
                text: '取消',
                btnClass: 'btn-default',
                keys: ['esc'],
                action: function () {

                }
            }
        }
    });
}

/**
 * 新增窗口
 */
function add() {
    $('#id').val('');
    $('#post_sign_name').val('');
    $('#modal-postSignInfo').modal('show');
    $('#editForm').data('bootstrapValidator').resetForm();
}

/**
 * 更新窗口
 */
function update() {
    var data = $('#table').bootstrapTable('getSelections');
    if (data.length > 1) {
        $.confirm({
            title: '提示',
            content: '只能修改一条记录！',
            autoClose: 'okAction|2000',
            escapeKey: 'okAction',
            type: 'orange',
            buttons: {
                close: {
                    text: '关闭',
                    btnClass: 'btn-orange'
                }
            }
        });
    } else {
        $('#id').val(data[0].id);
        $('#post_sign_name').val(data[0].post_sign_name);
        $('#modal-postSignInfo').modal('show');
        $('#editForm').data('bootstrapValidator').resetForm();
    }
}

/**
 * 搜索
 */
function search() {
    var post_sign_name = $('#search_name').val();
    if (post_sign_name.trim() != '') {
        var opt = {
            url: baseUrl + "/postSignInfo/queryByPage",
            silent: true,
            query: {
                name: post_sign_name
            }
        };
        $("#table").bootstrapTable('refresh', opt);
    }
}